﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Nissen_Common;
using Nissen_Model;
using PosHacker;
using System.Threading;
using PosHacker.PosException;
using PosHacker.Form;

namespace Nissen_Marking.Forms
{
    public partial class FormTestNames : Form
    {
        public POSHacker vPOSHacker;
        private Dictionary<string, string> vTestNames;
    	public  Form MainForm { get; set; }

        List<FormSetCourse> lFsc;
        string tmpTestId;
        public FormTestNames()
        {
            InitializeComponent();
			//var currentUser = ContextManager.Instance.CurrentUser;

			//this.vPOSHacker = POSHacker.GetInstance(currentUser.NissenSSO.NissenSSOCode, currentUser.NissenSSO.NissenSSOPWD);

        }

        private void FormTestNames_Load(object sender, EventArgs e)
        {

           // this.FormInit();
            this.label1.Text = ContextManager.Instance.CurrentUser.LoginName;
            this.label2.Text = ContextManager.Instance.CurrentUser.LoginSSO;
            this.label3.Text = ContextManager.Instance.CurrentUser.NissenSSO.NissenSSOCode;

        }

        private void FormInit() {
            //FormBrowser fb = new FormBrowser();
            //fb.Show();
            //return;

            this.radioButton1.Checked = true;

            this.buttonMarkingP.Enabled = false;
            this.buttonPCMark.Enabled = false;

            this.buttonSetCourseInfo.Enabled = false;
            lFsc = new List<FormSetCourse>();
            lFsc.Add(new FormSetCourse());
            this.dataGridView1.DataSource = lFsc;


            new Thread(new ThreadStart(Init)).Start();

            this.Enabled = false;

        
        }


        private void Init()
        {

            try
            {


                this.GetTestNames();
            }
            catch (PosException e)
            {
                //this.Invoke();
                MessageBox.Show(e.Message);
            }

        }

        private void GetTestNames()
        {

            this.vTestNames =
            this.vPOSHacker.GetTestNames();

            this.Invoke(new PosDelegate(SetTestNames));

        }

        private void SetTestNames()
        {

            this.comboBoxTestNames.Items.Clear();

			////Dictionary<string, string>.ValueCollection.Enumerator tmpEnumerator =
			////this.vTestNames.Values.GetEnumerator();
			//Dictionary<string, string>.KeyCollection.Enumerator tmpEnumerator =
			//this.vTestNames.Keys.GetEnumerator();
			//while (tmpEnumerator.MoveNext())
			//{
			//    this.comboBoxTestNames.Items.Add(tmpEnumerator.Current);
			//}
			TestNamesService testNamesService = new TestNamesService();
        	IList<Test> dbTests = testNamesService.GetDisplayTests();

			this.comboBoxTestNames.DisplayMember = "key";
			this.comboBoxTestNames.ValueMember = "value";

        	foreach (var vTestName in vTestNames)
        	{
                if (string.IsNullOrEmpty(vTestName.Value))//Please select a test name
				{
					this.comboBoxTestNames.Items.Add(vTestName);
					continue;
					
				}

        		foreach (var dbTest in dbTests)
        		{
        			if(dbTest.TestId ==vTestName.Value)
        			{
                        this.comboBoxTestNames.Items.Add(vTestName);
						break;
        			}
        		}
        		
        	}

            this.comboBoxTestNames.SelectedIndex = 0;

            //end init
            this.Enabled = true;
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            try
            {


            this.buttonMarkingP.Enabled = false;
            this.buttonPCMark.Enabled = false;

            this.buttonSetCourseInfo.Enabled = false;

			tmpTestId = ((KeyValuePair<string,string>) this.comboBoxTestNames.SelectedItem).Value.ToString();
        	
			//tmpTestId=
			//this.vTestNames[this.comboBoxTestNames.SelectedItem.ToString()];
            ContextManager.Instance._ContextProperties.TestName = this.comboBoxTestNames.Text;

            lFsc = this.vPOSHacker.GetMarkableList(tmpTestId);

			TestNamesService testNamesService = new TestNamesService();
			IList<SubjectCourse> subjectCourses = testNamesService.GetSubjectCourses(tmpTestId);

        	IList<FormSetCourse> formSetCourses = new List<FormSetCourse>();

			
			foreach (var formSetCourse in lFsc)
        	{
        		foreach (var subjectCourse in subjectCourses)
        		{
        				if(subjectCourse.SubjectCourseID == formSetCourse.subjectCourseTestId
							&& subjectCourse.SubjectCoursePage == formSetCourse.pageNum)
        				{
        					formSetCourses.Add(formSetCourse);
        				}
        		}
        	}

			if (subjectCourses.Count == 0)
			{
				this.dataGridView1.DataSource = lFsc;
			}
			else
			{
				this.dataGridView1.DataSource = formSetCourses;
                lFsc = (List<FormSetCourse>)formSetCourses;
			}

            //this.dataGridView1[0, 0].Value = true;

                

            //this.buttonMarkingP.Enabled =
            //this.vPOSHacker.SetTestCourseInfo(fsc);
            }
            catch (Exception ex){
                MessageBox.Show(ex.Message);
            }

        }

        private string GetRadioButtonValue() {

            if (this.radioButton1.Checked)
                return "1";

            return "2";
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex != -1)
            {
                this.buttonSetCourseInfo.Enabled = false;
                this.buttonMarkingP.Enabled = false;
                this.buttonPCMark.Enabled = false;

                bool tmpValue =
                (bool)this.dataGridView1[0, e.RowIndex].EditedFormattedValue;
                if (tmpValue)
                {
                    for(int i = 0; i < this.dataGridView1.RowCount; i++){

                        if(i != e.RowIndex){
                            this.dataGridView1[0,i].Value = false;
                        }
                        
                    }

                    this.buttonSetCourseInfo.Enabled = true;
                }
                    
                
            
            }
        }

        private void buttonSetCourseInfo_Click(object sender, EventArgs e)
        {
            //this.dataGridView1.
            bool tmpvalue = false;
            for (int i = 0; i < this.dataGridView1.RowCount; i++)
            {
                //this.dataGridView1.Columns[0].ValueType = new Type();
                if (this.dataGridView1[0, i].Value != null)
                {
                    tmpvalue = (bool)this.dataGridView1[0, i].Value;
                    if (tmpvalue)
                    {
                        Console.WriteLine(tmpvalue);
                        Console.WriteLine(this.dataGridView1.Rows[i].Cells["subjectCourseTestId"].Value);
                        Console.WriteLine(this.lFsc[i].subjectCourseTestId);


                        this.lFsc[i].testExecId = tmpTestId;
                        this.lFsc[i].reservationPriority = this.GetRadioButtonValue();

                        if (this.vPOSHacker.SetTestCourseInfo(this.lFsc[i]))
                        {
                        	ContextManager.Instance._FormSetCourse = lFsc[i];

                            this.buttonMarkingP.Enabled = true;
                            this.buttonPCMark.Enabled = true;
                        }
                    }

                }
            }

        }

        private void buttonMarkingP_Click(object sender, EventArgs e)
        {
            FormPartrailQuestion fpq = new FormPartrailQuestion();
            fpq.vParentForm = this;
            fpq.Show();//
            this.Hide();
            //using (FormPartrailQuestion fpq = new FormPartrailQuestion())
            //{
            //    this.Hide();
            //    var res =
            //        fpq.ShowDialog();

            //    if (DialogResult.Abort == res)
            //    {
            //        MessageBox.Show("该科目已被关闭，请重新选择！");
            //    }

            //    this.Show();
            //}
        }

		private void FormTestNames_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.MainForm.Show();
		}

        private void FormTestNames_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
                this.FormInit();
        }

		private void buttonPCMark_Click(object sender, EventArgs e)
		{
            if(this.PCMarkCheck())
            {
                var tempLink = vPOSHacker.GetPCMarkingActionLink();
                FormPCMarking formPcMarking = new FormPCMarking(tempLink);
                formPcMarking.vParentForm = this;
                formPcMarking.vPOSHacker = this.vPOSHacker;
                formPcMarking.Show();
                this.Hide();
            }else
            {
                MessageBox.Show("该试卷存在被限制的题目目，请使用分割採点！");
            }


		}

        private bool PCMarkCheck ()
        {
            List<FormSetScoringNo> lFSSN = vPOSHacker.GetMarkableQuestionsList();
            if (lFSSN == null)
                return true;

            var selectFormSetCourse =
                ContextManager.Instance._FormSetCourse;
            IList<MarkableQuestion> dbMarkableQuestions = new PartrailQuestionService().GetMarkableQuestions(selectFormSetCourse.subjectCourseTestId, selectFormSetCourse.pageNum);

            if (dbMarkableQuestions.Count == 0)
                return true;

            if (dbMarkableQuestions.Count == lFSSN.Count)
                return true;

            return false;
        }

        private void button1_Click(object sender, EventArgs e)
        {			
            
            var testNamesService = new TestNamesService();
			//IList<SubjectCourse> subjectCourses = new List<SubjectCourse>();
            IList<FormSetCourse> formSetCourses = new List<FormSetCourse>();
            foreach (var vTestName in this.vTestNames)
            {
                //foreach (var subjectCourse in testNamesService.GetSubjectCourses(vTestName.Value))
                foreach (var formSetCourse in this.vPOSHacker.GetMarkableList(vTestName.Value))
                {
                    formSetCourses.Add(formSetCourse);
                }
            }
            var form = new FormAnswerPaper();
            var tmpform = new FormDateSelect(form);
            tmpform.ShowDialog();
            form.SubjectCourseId = string.Empty;
            form.FormSetCourses = formSetCourses;
            //form.ParaDate = "20130615";
            form.ShowDialog();
        }
    }
}
